package com.lishe.lishe.lishe.utils;

import android.app.DownloadManager;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Environment;
import android.text.TextUtils;

import com.lishe.lishe.lishe.module.main.activity.LisheLodingActivity;
import com.tencent.mm.opensdk.openapi.IWXAPI;

import java.io.File;
import java.util.List;

/**
 * Created by lwa on 2017/9/4.
 * app工具类
 */

public class AppUtils {
    /**
     * 自定义文件名
     */
    private static final String appName = "lishe";

    public static final String APK_DOWNLOAD_URL = "https://admin.lishe.cn/Upload/appApk/礼舍.apk";

    //版本名
    public static String getVersionName(Context context) {
        return getPackageInfo(context).versionName;
    }

    //版本号
    public static int getVersionCode(Context context) {
        return getPackageInfo(context).versionCode;
    }

    private static PackageInfo getPackageInfo(Context context) {
        PackageInfo pi = null;

        try {
            PackageManager pm = context.getPackageManager();
            pi = pm.getPackageInfo(context.getPackageName(),
                    PackageManager.GET_CONFIGURATIONS);

            return pi;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return pi;
    }

    /**
     * 获取应用名
     *
     * @param context
     * @return
     */
    public static String getApplicationName(Context context) {
        if (!TextUtils.isEmpty(appName) || context == null) {
            return appName;
        }
        PackageManager packageManager = null;
        ApplicationInfo applicationInfo = null;
        try {
            packageManager = context.getPackageManager();
            applicationInfo = packageManager.getApplicationInfo(context.getPackageName(), 0);
        } catch (PackageManager.NameNotFoundException e) {
            applicationInfo = null;
        }
        String applicationName =
                (String) packageManager.getApplicationLabel(applicationInfo);
        return applicationName;
    }

    /**
     * 自动安装应用
     *
     * @param context
     * @param uri
     */
    public static void installApk(Context context, String uri) {
        // 核心是下面几句代码
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setDataAndType(Uri.fromFile(new File(uri)),
                "application/vnd.android.package-archive");
        context.startActivity(intent);
    }

    public static File getDownloadApkUri(Context context) {
        //设置下载文件的保存位置
        File file = new File(FileUtils.getAPKExternalFilesDir(context, Environment.DIRECTORY_DOWNLOADS), "礼舍.apk");
        return file;
    }

    public static long downLoadApkForDownloadManager(Context context, String downLoadUrl) {
        DownloadManager.Request request = new DownloadManager.Request(Uri.parse(downLoadUrl));
        request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
        request.setTitle("礼舍.apk");


        request.setDestinationUri(Uri.fromFile(getDownloadApkUri(context)));

        DownloadManager manager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
        return manager.enqueue(request);
    }

    /**
     * 检查是否安装了微信客户端
     * @param api
     * @param context
     * @return
     */
    public static boolean checkInstallWeiXin(IWXAPI api, Context context) {
        if (api.isWXAppInstalled() && api.isWXAppSupportAPI()) {
            return true;
        } else {
            final PackageManager packageManager = context.getPackageManager();// 获取packagemanager
            List<PackageInfo> pinfo = packageManager.getInstalledPackages(0);// 获取所有已安装程序的包信息
            if (pinfo != null) {
                for (int i = 0; i < pinfo.size(); i++) {
                    String pn = pinfo.get(i).packageName;
                    if (pn.equalsIgnoreCase("com.tencent.mm")) {
                        return true;
                    }
                }
            }
            return false;
        }
    }
}

